package com.hoshiibuy.global.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiibuy.common.exception.ServiceException;
import com.hoshiibuy.common.rpc.Response;
import com.hoshiibuy.global.entity.SysProperties;
import com.hoshiibuy.global.entity.param.SysPropertiesAddParam;
import com.hoshiibuy.global.entity.param.SysPropertiesQueryParam;
import com.hoshiibuy.global.entity.param.SysPropertiesUpdateParam;
import com.hoshiibuy.global.service.SysPropertiesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import com.hoshiibuy.common.utils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 属性 控制器
 * </p>
 *
 * @author campchen
 * @since 2019-04-27
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/sys-properties")
@Api(value = "全局基本定义", tags = {"属性全局管理"})
public class SysPropertiesController {
    @Autowired
    private SysPropertiesService sysPropertiesService;

    /**
    * @param param
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 分页查询属性
    */
    @ApiOperation(value = "分页查询属性")
    @PostMapping("/query/page")
    public Response page(@Valid SysPropertiesQueryParam param) {
        try {
            log.info("分页查询属性");
            QueryWrapper<SysProperties> queryWrapper = param.getQueryWrapper();
            IPage<SysProperties> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            return Response.data(sysPropertiesService.page(page, queryWrapper));
          } catch (ServiceException e) {
            return Response.failed(e.getError().getMsg());
          } catch (Exception e) {
            log.error("分页查询属性异常", e);
            return Response.failed("分页查询属性异常");
        }
    }
    /**
    * @param id
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 根据id查询属性
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询属性")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(sysPropertiesService.getById(id));
    }

    /**
    * @param param
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 新增
    */
    @PostMapping("/save")
    @ApiOperation(value = "新增属性")
    public Response save(@Valid SysPropertiesAddParam param) {
        try {
            SysProperties data = BeanUtils.covertType(param, SysProperties.class);
            return Response.data(sysPropertiesService.save(data));
        } catch (Exception e) {
            log.error("新增属性异常", e);
            return Response.failed();
        }
    }

    /**
    * @param param
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 修改属性
    */
    @PutMapping("/update")
    @ApiOperation(value = "修改属性")
    public Response update(@Valid SysPropertiesUpdateParam param) {
        try {
            SysProperties data = BeanUtils.covertType(param, SysProperties.class);
            return Response.data(sysPropertiesService.updateById(data));
        } catch (Exception e) {
            log.error("修改属性异常", e);
            return Response.failed();
        }
    }
}
